// created by noellynch
// Jun 5, 2009

package com.sceneworker.app.help;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.DefaultListModel;
import javax.swing.JEditorPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.EditorKit;

import net.miginfocom.swing.MigLayout;

import com.acarter.scenemonitor.util.Util;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.util.xml.TagCallBackLoader;
import com.sceneworker.util.xml.TagCallbackStruct;

public class HelpPanel extends JPanel {
	private		JList			mcl_topicList;
	private		JEditorPane		mcl_helpTopicArea;
	
	static	private		ArrayList<HelpTopic>	mcla_topics;
	
	public	HelpPanel() {
		if(mcla_topics == null) {
			loadTopics();
		}
		
		setLayout(new MigLayout("inset 5"));
		
		initialiseTopicPanel();
		
		initialiseHelpTopicArea();
	}
	
	private	void	updateTopicsList(String str_q) {
		DefaultListModel		lcl_model = new DefaultListModel();
		String					lstr_q = str_q.toLowerCase();
		
		for(HelpTopic lcl_topic : mcla_topics) {
			String		lstr_topic = lcl_topic.mstr_title.toLowerCase();
			
			if(lstr_topic.indexOf(lstr_q) != -1) {
				lcl_model.addElement(lcl_topic);
			}
		}
		
		mcl_topicList.setModel(lcl_model);
		repaint();
	}
	
	private	void	initialiseTopicPanel() {
		JPanel		lcl_panel = new JPanel(new MigLayout("inset 5"));
		
		final	JTextField		lcl_queryField = SwingComponentFactory.makeATextField();
		lcl_queryField.setText("enter query here");
		lcl_queryField.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
			}

			public void keyReleased(KeyEvent e) {
				if(lcl_queryField.getText().equals("enter query here")) {
					lcl_queryField.setText("");
				}
				
				updateTopicsList(lcl_queryField.getText());
			}

			public void keyTyped(KeyEvent e) {
			}
		});
		
		lcl_panel.add(lcl_queryField, "wrap");
		
		DefaultListModel		lcl_model = new DefaultListModel();
		
		for(HelpTopic lcl_topic : mcla_topics) {
			lcl_model.addElement(lcl_topic);
		}
		
		mcl_topicList = new JList();
		mcl_topicList.setModel(lcl_model);
		mcl_topicList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				HelpTopic		lcl_h = (HelpTopic)mcl_topicList.getSelectedValue();
				if(lcl_h == null) { return; }
				
				try {
					mcl_helpTopicArea.setPage(HelpPanel.class.getClassLoader().getResource(SceneWorkerGlobals.DATA_DIR + lcl_h.mstr_location));
				} catch (IOException e1) {
					SimpleAlert.doAlert("Could not open help file for " + lcl_h.mstr_title + ".", null);
				}
			}
		});
		
		JScrollPane		lcl_scroll = new JScrollPane();
		lcl_scroll.setViewportView(mcl_topicList);
		lcl_panel.add(lcl_scroll, "width 100%, height 100%");
		
		add(lcl_panel, "width 30%, height 100%");
	}
	
	private	void	initialiseHelpTopicArea() {
		mcl_helpTopicArea = new JEditorPane();
		mcl_helpTopicArea.setEditable(false);
		
		EditorKit		lcl_ek = mcl_helpTopicArea.getEditorKit();
		System.out.println("kit = " + lcl_ek.toString());
		
		mcl_helpTopicArea.addHyperlinkListener(new HyperlinkListener() {
			public void hyperlinkUpdate(HyperlinkEvent e) {
				if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
					System.out.println("url = " + e.getURL().toString());
					Util.openURL(e.getURL().toString());
				}
				
			}
		});
		
		JScrollPane		lcl_scroll = new JScrollPane();
		lcl_scroll.setViewportView(mcl_helpTopicArea);
		add(lcl_scroll, "width 70%, height 100%");
	}
	
	private	void	loadTopics() {
		mcla_topics = new ArrayList<HelpTopic>();
		
		TagCallBackLoader		lcl_loader = new TagCallBackLoader(this);
		lcl_loader.addCallback("topic", new TagCallbackStruct(TopicTag.class.getName()));
		
		URL		lcl_loc = HelpPanel.class.getClassLoader().getResource(SceneWorkerGlobals.DATA_DIR + "helpFiles/helpTopics.xml");
		System.out.println("url = " + lcl_loc.toString());
		TagCallBackLoader.loadXMLFile(lcl_loc, lcl_loader);
		
		Collections.sort(mcla_topics, new Comparator<HelpTopic>() {
			public int compare(HelpTopic cl_t1, HelpTopic cl_t2) {
				return cl_t1.mstr_title.compareToIgnoreCase(cl_t2.mstr_title);
			}
			
		});
	}
	
	public	void	addTopic(HelpTopic cl_topic) {
		mcla_topics.add(cl_topic);
		
	}
	
	public	HelpTopic		newHelpTopic() {
		return new HelpTopic();
	}
	
	public	class	HelpTopic {
		public	String	mstr_title;
		public	String	mstr_location;
		
		public	HelpTopic() {
		}

		@Override
		public String toString() {
			return mstr_title;
		}
	}
}
